package com.newangels.service.impl;

import com.newangels.service.AgentManagerService;
import com.newangels.util.DBUtil;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author: TangLiang
 * @date: 2020/5/11 16:54
 * @since: 1.0
 */
public class AgentManagerServiceImpl implements AgentManagerService {
    DBUtil dbUtil = DBUtil.getDBUtil();

    private static class SingletonHolder {
        private static final AgentManagerServiceImpl INSTANCE = new AgentManagerServiceImpl();
    }

    public static final AgentManagerServiceImpl getAgentManagerServiceImpl() {
        return AgentManagerServiceImpl.SingletonHolder.INSTANCE;
    }

    private AgentManagerServiceImpl() {
        //防止通过反射获取多个对象
        if (null != AgentManagerServiceImpl.SingletonHolder.INSTANCE)
            throw new RuntimeException(AgentManagerServiceImpl.class.getName() + "已存在,不可再创建对象");
    }

    @Override
    public List<Map<String, Object>> selectAgents(String AGENT_ID_, String AGENT_CODE_, String AGENT_NAME_, String AGENT_URL_, String AGENT_ARGUMENTS_, String AGENT_STATE_, String STATE_, Integer AGENT_COST_, String AGENT_SOURCE_, Integer page, Integer limit) {
        String sql = buildSql(false, AGENT_ID_, AGENT_CODE_, AGENT_NAME_, AGENT_URL_, AGENT_ARGUMENTS_, AGENT_STATE_, STATE_, AGENT_COST_, AGENT_SOURCE_);
        if (page != null && limit != null && limit > 0) {// 分页
            int start = (page - 1) * limit + 1;
            int end = page * limit;
            sql = "SELECT * FROM (SELECT FULLTABLE.*, ROWNUM RN FROM (" + sql + ") FULLTABLE WHERE ROWNUM <= " + end + ") WHERE RN >= " + start;
        }
        return dbUtil.executeQuery(sql);
    }

    @Override
    public int getcount(String AGENT_ID_, String AGENT_CODE_, String AGENT_NAME_, String AGENT_URL_, String AGENT_ARGUMENTS_, String AGENT_STATE_, String STATE_, Integer AGENT_COST_, String AGENT_SOURCE_) {
        String sql = buildSql(true, AGENT_ID_, AGENT_CODE_, AGENT_NAME_, AGENT_URL_, AGENT_ARGUMENTS_, AGENT_STATE_, STATE_, AGENT_COST_, AGENT_SOURCE_);
        return dbUtil.getCount(sql);
    }

    @Override
    public int updateSTATE(String AGENT_CODE_, String AGENT_STATE_) {
        String sql = "UPDATE TF_AGENTMANAGE SET AGENT_STATE_ = ? WHERE AGENT_CODE_ = ?";
        return dbUtil.executeUpdate(sql, AGENT_STATE_, AGENT_CODE_);
    }

    private String buildSql(boolean count, String AGENT_ID_, String AGENT_CODE_, String AGENT_NAME_, String AGENT_URL_, String AGENT_ARGUMENTS_, String AGENT_STATE_, String STATE_, Integer AGENT_COST_, String AGENT_SOURCE_) {
        String sql;
        if (count) {
            sql = "SELECT COUNT(ROWID) FROM TF_AGENTMANAGE WHERE 1 = 1";
        } else {
            sql = "SELECT * FROM TF_AGENTMANAGE WHERE 1 = 1";
        }
        StringBuffer sb = new StringBuffer(sql);
        if (StringUtils.isNotEmpty(AGENT_ID_)) {
            sb.append(" AND AGENT_ID_ = '" + AGENT_ID_ + "'");
        }
        if (StringUtils.isNotEmpty(AGENT_CODE_)) {
            sb.append(" AND AGENT_CODE_ = '" + AGENT_CODE_ + "'");
        }
        if (StringUtils.isNotEmpty(AGENT_NAME_)) {
            sb.append(" AND AGENT_NAME_ LIKE '%" + AGENT_NAME_ + "%'");
        }
        if (StringUtils.isNotEmpty(AGENT_URL_)) {
            sb.append(" AND AGENT_URL_ LIKE '%" + AGENT_URL_ + "%'");
        }
        if (StringUtils.isNotEmpty(AGENT_ARGUMENTS_)) {
            sb.append(" AND AGENT_ARGUMENTS_ = '" + AGENT_ARGUMENTS_ + "'");
        }
        if (StringUtils.isNotEmpty(AGENT_STATE_)) {
            sb.append(" AND AGENT_STATE_ = '" + AGENT_STATE_ + "'");
        }
        if (StringUtils.isNotEmpty(STATE_)) {
            sb.append(" AND STATE_ = '" + STATE_ + "'");
        }
        if (AGENT_COST_ != null) {
            sb.append(" AND AGENT_COST_ = '" + AGENT_COST_ + "'");
        }
        if (StringUtils.isNotEmpty(AGENT_SOURCE_)) {
            sb.append(" AND AGENT_SOURCE_ = '" + AGENT_SOURCE_ + "'");
        }
        sql = sb.toString();
        return sql;
    }

    @Override
    public int insertAgentManager(String AGENT_ID_, String AGENT_CODE_, String AGENT_NAME_, String AGENT_URL_, String AGENT_ARGUMENTS_, String AGENT_STATE_, String STATE_, Integer AGENT_COST_, String AGENT_SOURCE_, String AGENT_EXCEPTION_) {
        String sql = "INSERT INTO TF_AGENTMANAGE VALUES(?,?,?,?,?,?,?,?,?,?)";
        return dbUtil.executeUpdate(sql, AGENT_ID_, AGENT_CODE_, AGENT_NAME_, AGENT_URL_, AGENT_ARGUMENTS_, AGENT_STATE_, STATE_, AGENT_COST_, AGENT_SOURCE_, AGENT_EXCEPTION_);
    }

    @Override
    public Map<String, Object> loadAgent(String AGENT_ID_) {
        String sql = "SELECT * FROM TF_AGENTMANAGE WHERE AGENT_ID_ = ?";
        List<Map<String, Object>> list = dbUtil.executeQuery(sql, AGENT_ID_);
        return list.size() == 1 ? list.get(0) : null;
    }

    @Override
    public int updateAgent(String AGENT_ID_, String AGENT_CODE_, String AGENT_NAME_, String AGENT_URL_, String AGENT_ARGUMENTS_, Integer AGENT_COST_, String AGENT_SOURCE_) {
        String sql = "UPDATE TF_AGENTMANAGE SET AGENT_CODE_ =?,AGENT_NAME_ = ?,AGENT_URL_ = ?,AGENT_ARGUMENTS_ = ?,AGENT_COST_ = ?,AGENT_SOURCE_ = ? WHERE AGENT_ID_ = ?";
        return dbUtil.executeUpdate(sql, AGENT_CODE_, AGENT_NAME_, AGENT_URL_, AGENT_ARGUMENTS_, AGENT_COST_, AGENT_SOURCE_, AGENT_ID_);
    }

    @Override
    public int deleteAgent(String AGENT_ID_) {
        String sql = "DELETE FROM TF_AGENTMANAGE WHERE AGENT_ID_ = ?";
        return dbUtil.executeUpdate(sql, AGENT_ID_);
    }

    @Override
    public int deleteAgentList(List<String> AgentList) {
        String sql = "DELETE FROM TF_AGENTMANAGE WHERE AGENT_ID_ IN ('0'";
        StringBuffer sb = new StringBuffer(sql);
        for (String s : AgentList) {
            sb.append(",'" + s + "'");
        }
        sb.append(")");
        sql = sb.toString();
        return dbUtil.executeUpdate(sql);
    }

    @Override
    public int updateAllAgentState(String AGENT_STATE_, String AGENT_STATE_1) {
        String sql = "UPDATE TF_AGENTMANAGE SET AGENT_STATE_ = ? WHERE AGENT_STATE_ = ?";
        return dbUtil.executeUpdate(sql, AGENT_STATE_, AGENT_STATE_1);
    }
}
